package com.ruoyi.hospital.service.doctor.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hospital.domain.doctor.DocScheduleDetail;
import com.ruoyi.hospital.mapper.doctor.DocScheduleDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.doctor.DocScheduleMapper;
import com.ruoyi.hospital.domain.doctor.DocSchedule;
import com.ruoyi.hospital.service.doctor.IDocScheduleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 医生排班Service业务层处理
 * 
 * @author wendao
 * @date 2024-11-13
 */
@Service
public class DocScheduleServiceImpl implements IDocScheduleService 
{
    @Autowired
    private DocScheduleMapper docScheduleMapper;
    @Autowired
    private DocScheduleDetailMapper docScheduleDetailMapper;

    /**
     * 查询医生排班
     * 
     * @param id 医生排班主键
     * @return 医生排班
     */
    @Override
    public DocSchedule selectDocScheduleById(Integer id)
    {
        return docScheduleMapper.selectDocScheduleById(id);
    }

    @Override
    public DocSchedule selectDocSchedule(DocSchedule docSchedule) {
        return docScheduleMapper.selectDocSchedule(docSchedule);
    }

    /**
     * 查询医生排班列表
     * 
     * @param docSchedule 医生排班
     * @return 医生排班
     */
    @Override
    public List<DocSchedule> selectDocScheduleList(DocSchedule docSchedule)
    {
        return docScheduleMapper.selectDocScheduleList(docSchedule);
    }

    /**
     * 新增医生排班
     * 
     * @param docSchedule 医生排班
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDocSchedule(DocSchedule docSchedule)
    {
        docSchedule.setCreateTime(DateUtils.getNowTimeSecond());
        int i = docScheduleMapper.insertDocSchedule(docSchedule);
        List<DocScheduleDetail> docScheduleDetailList = docSchedule.getDocScheduleDetailList();
        if(docScheduleDetailList!=null){
            for (DocScheduleDetail docScheduleDetail:docScheduleDetailList){
                docScheduleDetail.setSid(docSchedule.getId());
                docScheduleDetail.setScheduleDate(docSchedule.getScheduleDate());
                docScheduleDetailMapper.insertDocScheduleDetail(docScheduleDetail);
            }
        }

        return i;
    }

    /**
     * 修改医生排班
     * 
     * @param docSchedule 医生排班
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDocSchedule(DocSchedule docSchedule)
    {
        docSchedule.setUpdateTime(DateUtils.getNowTimeSecond());
        int i = docScheduleMapper.updateDocSchedule(docSchedule);

        List<DocScheduleDetail> docScheduleDetailList = docSchedule.getDocScheduleDetailList();
        if(docScheduleDetailList!=null){
            for (DocScheduleDetail docScheduleDetail:docScheduleDetailList){
                docScheduleDetailMapper.updateDocScheduleDetail(docScheduleDetail);
            }
        }

        return i;
    }

    /**
     * 批量删除医生排班
     * 
     * @param ids 需要删除的医生排班主键
     * @return 结果
     */
    @Override
    public int deleteDocScheduleByIds(Integer[] ids)
    {

        return docScheduleMapper.deleteDocScheduleByIds(ids);
    }

    /**
     * 删除医生排班信息
     * 
     * @param id 医生排班主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDocScheduleById(Integer id)
    {
        //同时删除detail
        docScheduleDetailMapper.deleteDocScheduleDetailBySid(id);
        return docScheduleMapper.deleteDocScheduleById(id);
    }
}
